package miniproj.services.serialization;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.util.*;

import miniproj.models.room.*;

/**
 * A XmlAdapter to serlialize a List<Room>
 * 
 * @author Baptiste Viale
 * 
 */
public class SerializedRoom extends XmlAdapter<RoomsType, List<Room>> {

	@Override
	public RoomsType marshal(List<Room> l) throws Exception {
		List<Amphi> amphis = new LinkedList<Amphi>();
		List<ClassRoom> classrooms = new LinkedList<ClassRoom>();
		List<MeetingRoom> meetingrooms = new LinkedList<MeetingRoom>();
		List<TDRoom> tdrooms = new LinkedList<TDRoom>();
		List<TPRoom> tprooms = new LinkedList<TPRoom>();

		for (Room r : l) {
			if (r instanceof Amphi)
				amphis.add((Amphi) r);
			else if (r instanceof ClassRoom)
				classrooms.add((ClassRoom) r);
			else if (r instanceof MeetingRoom)
				meetingrooms.add((MeetingRoom) r);
			else if (r instanceof TDRoom)
				tdrooms.add((TDRoom) r);
			else if (r instanceof TPRoom)
				tprooms.add((TPRoom) r);
		}

		return new RoomsType(amphis, classrooms, meetingrooms, tdrooms, tprooms);
	}

	@Override
	public List<Room> unmarshal(RoomsType rt) throws Exception {
		List<Room> l = new LinkedList<Room>();

		l.addAll(rt.getAmphis());
		l.addAll(rt.getClassrooms());
		l.addAll(rt.getMeetingrooms());
		l.addAll(rt.getTdrooms());
		l.addAll(rt.getTprooms());

		return l;
	}
}
